package top.lyjwn.todo.weinxin.api.bjnews;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.lyjwn.todo.weinxin.api.url.WXBjnewsUrl;
import top.lyjwn.todo.common.utils.http.HttpUtil;

/**
 * 刷新access_token（如果需要）
 * <p>
 * 由于access_token拥有较短的有效期，当access_token超时后，可以使用refresh_token进行刷新，
 * refresh_token有效期为30天，当refresh_token失效之后，需要用户重新授权。
 *
 * @author luoyujie
 */
@Component
public class RefreshUserAccessToken {

    @Autowired
    private WXBjnewsUrl wxBjnewsUrl;


    public RefreshUserAccessToken() {
    }


    public String getUrl(String refreshToken) {
        String url = wxBjnewsUrl.getUSER_REFRESH_TOKEN().replace("#{REFRESH_TOKEN}", refreshToken);
        return url;
    }

    public String getResultString(String refreshToken) {
        String result = HttpUtil.request(getUrl(refreshToken), HttpUtil.METHOD_GET);
        return result;
    }

    public JSONObject getResultJSONObject(String refreshToken) {
        String resultString = this.getResultString(refreshToken);
        return JSONObject.parseObject(resultString);
    }

}
